using System;
using System.Collections.Generic;
using System.Reflection;
using Cysharp.Threading.Tasks;
using GameLogic;
using GameLogic.GameLogic;
using Luban;
using TEngine;
using TEngine.Localization.SimpleJSON;
using UnityEngine;
#pragma warning disable CS0436


/// <summary>
/// 游戏App。
/// </summary>
public partial class GameApp
{
    private static List<Assembly> _hotfixAssembly;

    /// <summary>
    /// 热更域App主入口。
    /// </summary>
    /// <param name="objects"></param>
    public static void Entrance(object[] objects)
    {
        GameEventHelper.Init();
        _hotfixAssembly = (List<Assembly>)objects[0];
        Log.Warning("======= 看到此条日志代表你成功运行了热更新代码 =======");
        Log.Warning("======= Entrance GameApp =======");
        Utility.Unity.AddDestroyListener(Release);
        StartGameLogic();
    }

    /// <summary>
    /// 开始游戏业务层逻辑。
    /// <remarks>显示UI、加载场景等。</remarks>
    /// </summary>
    private static async UniTaskVoid StartGameLogic()
    {
        ConfigSystem.Instance.Load();

        // 等待用户信息初始化完成
        await UserInfo.Instance.InitUserInfoDataAsync();

        Debug.Log("成功进入我的热更新");
        UIModule.Instance.Active();
        StartBattleRoom().Forget();
    }

    private static async UniTaskVoid StartBattleRoom()
    {
        try
        {
            await GameModule.Scene.LoadSceneAsync("MenuScene");
            await UIModule.Instance.ShowUIAsyncAwait<MenuUI>();
        }
        catch (Exception ex)
        {
            Debug.LogError($"StartBattleRoom 执行出错: {ex.Message}");
        }
    }


    private static void Release()
    {
        SingletonSystem.Release();
        Log.Warning("======= Release GameApp =======");
    }
}